package com.gmail.dengtao.joe.group.channel;

import org.junit.Test;

import com.gmail.dengtao.joe.group.channel.Channel;
import com.gmail.dengtao.joe.group.channel.ChannelListener;
import com.gmail.dengtao.joe.group.channel.impl.TCPChannel;
import com.gmail.dengtao.joe.group.exception.ChannelException;
import com.gmail.dengtao.joe.group.member.Member;
import com.gmail.dengtao.joe.group.member.MembershipListener;

public class ChannelTest4 {

	@Test
	public void test() throws ChannelException {
		
		// Create a channel
		Channel channel = new TCPChannel("127.0.0.1", 7003);
		
		// Create my listeners
		ChannelListener channelListener = new MyChannelListener();
		MembershipListener membershipListener = new MyMembershipListener();
		
		// Attach the listeners to the channel
		channel.setChannelListener(channelListener);
		channel.setMembershipListener(membershipListener);
		
		// Config channel receiver
		channel.getChannelReceiver().setSelectTimeout(100);
		channel.getChannelReceiver().setIdleTimeForRead(5);
		channel.getChannelReceiver().setReceiveBufferSize(512);
		
		// Config channel sender
		channel.getChannelSender().setSelectTimeout(100);
		channel.getChannelSender().setIdleTimeForWrite(5);
		channel.getChannelSender().setSendBufferSize(512);

		// Open channel
		channel.open();
		for (int i = 0; i < 1; i++) {
			channel.addMember(new Member("127.0.0.1", 7000 + i));
		}
		System.out.println(channel.getId());
		// try { Thread.sleep(3000); } catch (InterruptedException e) { }
		// channel.close();
		for(;;) { try { Thread.sleep(10000); } catch (InterruptedException e) { } }
	}

}